#include "dialog_main.h"
#include "bmp_ants.h"
#include "bmp_battery.h"
#include "bmp_light.h"
#include "bmp_rssi.h"
#include "bmp_sdcard.h"
#include "bmp_speeker.h"
#include "bmp_sport.h"
#include "stdio.h"
#include "bmp_backgrounds.h"

#include "dialog_cali.h"
#include "dialog_bright.h"

MainDialog::MainDialog(HWND parent):Dialog(parent)
{
    name ="MainDialog";

    setSize(212,64);
    setupWidgets();
}

MainDialog::~MainDialog()
{
    cleanWidgets();
}

void MainDialog::setupWidgets()
{
    short x, y;

    //setup widgets
    pButtonDate =new  Button(this, "AUG 24");
    pButtonTime =new Button(this, "00:00");
    pBitmapButtonLight =new BitmapButton(this, (Bitmap *)&bitmapLight);
    pBitmapButtonContrast =new BitmapButton(this, (Bitmap *)&bitmapContrast);
    pBitmapButtonSD =new BitmapButton(this, (Bitmap *)&bitmapSdcardVacant);
    pBitmapButtonSPort =new BitmapButton(this, (Bitmap *)&bitmapSPortIdle);
    pBitmapButtonSpeeker =new BitmapButton(this, (Bitmap *)&bitmapSpeekerOn);
    pBitmapButtonAntenna =new BitmapButton(this, (Bitmap *)&bitmapAntOK);
    pBitmapButtonBattery =new BitmapButton(this, (Bitmap *)&bitmapBattery10);
    pBitmapButtonRSSI =new BitmapButton(this, (Bitmap *)&bitmapRSSI0);
    pBitmapButtonVolt =new BitmapButton(this, (Bitmap *)&bitmapBattery10);
    pCheckBox = new CheckBox(this);
    pListMode =new List(this);

    pListMode->addItem("item1");
    pListMode->addItem("item2");
    pListMode->addItem("item3");
    pListMode->addItem("item4");

    //name widgets
    //all widgets have default names
    pButtonDate->name ="ButtonDate";
    pButtonTime->name = "ButtonTime";
    pBitmapButtonLight->name ="ButtonLight";
    pBitmapButtonContrast->name ="ButtonContrast";

    //set widgets location
    x=0; y=0;
    pButtonDate->setPosition(x,y+1); x+=40;
    pButtonTime->setPosition(x, y+1); x+=34;
    pBitmapButtonLight->setPosition(x,y); x+=12;
    pBitmapButtonContrast->setPosition(x,y); x+=12;
    pBitmapButtonSD->setPosition(x,y); x+=15;
    pBitmapButtonSPort->setPosition(x,y); x+=15;
    pBitmapButtonSpeeker->setPosition(x,y); x+=15;
    pBitmapButtonAntenna->setPosition(x,y); x+=15;
    pBitmapButtonBattery ->setPosition(x,y); x+=15;
    pBitmapButtonRSSI->setPosition(x,y); x+=20;
    pBitmapButtonVolt->setPosition(x,y); x+=20;

    x=0; y=12;
    pEditName =new  Edit(this, "airp");
    pEditName->name ="EditName";

    pEditName->setPosition(x,y);
    pCheckBox->setPosition(x+20, y);
    pListMode->setPosition(x+40,y);

    //connect signal slots
    connect(&pButtonDate->signal[SIGNAL_BUTTON_UP], this);
    connect(&pBitmapButtonLight->signal[SIGNAL_BMP_BUTTON_UP], this);
    connect(&pEditName->signal[SIGNAL_EDIT_CHANGED], this);
    connect(&pEditName->signal[SIGNAL_EDIT_LEAVE_FOCUS], this);
    connect(&pListMode->signal[SIGNAL_LIST_LEAVE_FOCUS], this);
}


void MainDialog::cleanWidgets()
{
    //delete widgets
    delete pButtonDate;
    delete pButtonTime;
    delete pBitmapButtonLight;
    delete pBitmapButtonContrast;
    delete pBitmapButtonSD;
    delete pBitmapButtonSPort;
    delete pBitmapButtonSpeeker;
    delete pBitmapButtonAntenna;
    delete pBitmapButtonVolt;
    delete pBitmapButtonRSSI;
    delete pBitmapButtonBattery;

    delete pEditName;

}


void MainDialog::setupGeometry()
{
}

void MainDialog::draw(Rect *r)
{
#if 1
    //draw this window

    //fill rect with a bitmap
   short x,y;
    for( y=rect.y0; y<rect.y1; y+=bitmapBG3.h)
    {
        for(x=rect.x0; x<rect.x1; x+=bitmapBG3.w)
        {
            pDC->drawBmp(x, y, x+bitmapBG3.w, y+bitmapBG3.h, bitmapBG3.data);
         }
    }
#else
//    pDC->fillRect(rect.x0, rect.y0, rect.x1, rect.y1, 0x00);
    pDC->fillRect(r->x0, r->y0, r->x1, r->y1, 0x0);
#endif

    pDC->dirty(max(r->x0,rect.x0), max(r->y0,rect.y0), min(r->x1,rect.x1), min(r->y1,rect.y1));
    Widget::draw(r);
}


int MainDialog::buttonDateDown()
{
   printf("buttonDate Down\n");

   extern CaliDialog *pCaliDialog;

    ///< dynamic created dialog
   pCaliDialog =new CaliDialog(this);
   pCaliDialog->setPosition(20,2);
   pCaliDialog->run();

   delete pCaliDialog;
   pCaliDialog =0;
}

int MainDialog::buttonTimeDown()
{
   printf("buttonDate up\n");
}

int MainDialog::buttonLightDown()
{
   printf("buttonLight Down\n");
   BrightDialog *pBrightDialog;

   pBrightDialog =new BrightDialog(this);
   pBrightDialog->run();

   delete pBrightDialog;
   pBrightDialog =0;
}

int MainDialog::buttonContrastDown()
{
   printf("buttonLight up\n");
}

int MainDialog::nameChanged()
{
   printf("name is:%s\n", pEditName->getText());
}

int MainDialog::eventHandler(HWND hWnd, int iMsg, WPARAM wParam, LPARAM lParam)
{
    //signal handling
    switch(iMsg)
    {
        case MSG_SIGNAL:
        if(SIGNAL_MATCH(wParam, &pButtonDate->signal[SIGNAL_BUTTON_UP]) )
        {
            buttonDateDown();
            return 0;
        }
        
        if(SIGNAL_MATCH(wParam, &pBitmapButtonLight->signal[SIGNAL_BMP_BUTTON_UP]) )
        {
            buttonLightDown();
            return 0;
        }

        if(SIGNAL_MATCH(wParam, &pEditName->signal[SIGNAL_EDIT_CHANGED]))
        {
            nameChanged();
            return 0;
        }

        if(SIGNAL_MATCH(wParam, &pEditName->signal[SIGNAL_EDIT_LEAVE_FOCUS]))
        {
            printf("edit name :leave focus\n");
            return 0;
        }

        if(SIGNAL_MATCH(wParam, &pListMode->signal[SIGNAL_LIST_LEAVE_FOCUS]))
        {
            printf("listMode :leave focus\n");
            printf("index =%x\n",pListMode->index);
            return 0;
        }
        break;
        
    case MSG_USER_CALIBRATE_DOWN:
        pButtonDate->setText("MAY:25");
        return 0;
        break;

    }


    //default event handler
    return Dialog::eventHandler(hWnd, iMsg, wParam, lParam);
}





